﻿$(document).ready(function () {

    $.validator.addMethod(
        "regexp",
        function (value, element, string) {
            var regex;

            switch (string) {
                case 'account':
                    regex = /^[A-Za-z][A-Za-z0-9_]+$/;
                    $.validator.messages.regexp = "Start by a letter, only digits, letters and _ ";
                    break;
                case 'password':
                    regex = /^[A-Za-z0-9]+$/;
                    $.validator.messages.regexp = "Only digits and letters";
                    break;
                case 'fname':
                    regex = /^[A-Za-z][A-Za-z ]+[A-Za-z]$/;
                    $.validator.messages.regexp = "Only letters and spacse, begin and end by a letter";
                    break;
                case 'lname':
                    regex = /^[A-Za-z]+$/;
                    $.validator.messages.regexp = "Only letters";
                    break;
                default:
                    regex = /^[A-Za-z0-9]+$/;
                    $.validator.messages.regexp = "Invalid validate parameter";
                    break;
            }
            var re = new RegExp(regex);
            return this.optional(element) || re.test(value);
        },
        ""
    );

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var pubEditForm = $('#publisher_edit_form');
    var errorPubEdit = $('.alert-error', pubEditForm);

    pubEditForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            email: {
                email: true,
                required: true
            },
            companyname: {
                required: true
            }
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            error.insertAfter(element); // for other inputs, just perform default behavoir
        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorPubEdit.show();
            App.scrollTo(errorPubEdit, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorPubEdit.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var pubAddForm = $('#publisher_add_form');
    var errorPubAdd = $('.alert-error', pubAddForm);

    pubAddForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            username: {
                minlength: 6,
                regexp: "account",
                required: true
            },
            password: {
                minlength: 6,
                regexp: "password",
                required: true
            },
            confirmpassword: {
                equalTo: "#password",
                regexp: "password",
                minlength: 6,
                required: true
            },
            email: {
                email: true,
                required: true
            },
            companyname: {
                required: true
            },
            packagepub: {
                required: true
            }
        },

        messages: {
            packagepub: {
                required: "Please select a gender choice."
            },
            confirmpassword: {
                equalTo: "Your confirm password incorrect."
            }
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            if (element.attr("name") == "packagepub") { // for chosen elements, need to insert the error after the chosen container
                error.insertAfter("#frm_packagepub_error");
            } else {
                error.insertAfter(element); // for other inputs, just perform default behavoir
            }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorPubAdd.show();
            App.scrollTo(errorPubAdd, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorPubAdd.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var memEditForm = $('#member_edit_form');
    var errorMemEdit = $('.alert-error', memEditForm);

    memEditForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            firstname: {
                regexp: "fname",
                required: true
            },
            lastname: {
                regexp: "lname",
                required: true
            },
            gender: {
                required: true
            },
            email: {
                email: true,
                required: true
            }
        },
        messages: {
            gender: {
                required: "Please select a gender choice."
            }
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            if (element.attr("name") == "gender") { // for chosen elements, need to insert the error after the chosen container
                error.insertAfter("#frm_gender_error");
            } else {
                error.insertAfter(element); // for other inputs, just perform default behavoir
            }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorMemEdit.show();
            App.scrollTo(errorMemEdit, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorMemEdit.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var memAddForm = $('#member_add_form');
    var errorMemAdd = $('.alert-error', memAddForm);

    memAddForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            username: {
                minlength: 6,
                regexp: "account",
                required: true
            },
            password: {
                minlength: 6,
                regexp: "password",
                required: true
            },
            confirmpassword: {
                equalTo: "#password",
                regexp: "password",
                minlength: 6,
                required: true
            },
            firstname: {
                regexp: "fname",
                required: true
            },
            lastname: {
                regexp: "lname",
                required: true
            },
            gender: {
                required: true
            },
            email: {
                email: true,
                required: true
            }
        },
        messages: {
            gender: {
                required: "Please select a gender choice."
            },
            confirmpassword: {
                equalTo: "Your confirm password incorrect."
            }
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            if (element.attr("name") == "gender") { // for chosen elements, need to insert the error after the chosen container
                error.insertAfter("#frm_gender_error");
            } else {
                error.insertAfter(element); // for other inputs, just perform default behavoir
            }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorMemAdd.show();
            App.scrollTo(errorMemAdd, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorMemAdd.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var catAddForm = $('#addcat_frm');

    catAddForm.validate({
        errorClass: "help-block animation-slideDown",
        errorElement: "span",
        errorPlacement: function (e, a) {
            a.parents(".form-group > div").append(e)
        },
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success has-error").addClass("has-error"),
            $(e).closest(".help-block").remove()
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-success has-error"),
            e.closest(".help-block").remove()
        }, rules: {
            val_name: {
                required: !0,
                minlength: 2
            },
            val_icon: {
                required: !0,
                minlength: 2
            },
            val_description: {
                required: !0
            }
        }, messages: {
            val_name: {
                required: "Bắt Buộc",
                minlength: "Chứa Ít Nhất 2 Ký Tự"
            },
            val_icon: {
                required: "Bắt Buộc"
            },
            val_description: {
                required: "Bắt Buộc"
            }
        }
    });

    var catEditForm = $('#editcat-frm');

    catEditForm.validate({
        errorClass: "help-block animation-slideDown",
        errorElement: "span",
        errorPlacement: function (e, a) {
            a.parents(".form-group > div").append(e)
        },
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success has-error").addClass("has-error"),
            $(e).closest(".help-block").remove()
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-success has-error"),
            e.closest(".help-block").remove()
        }, rules: {
            val_name: {
                required: !0,
                minlength: 2
            },
            val_icon: {
                required: !0,
                minlength: 2
            },
            val_description: {
                required: !0
            }
        }, messages: {
            val_name: {
                required: "Bắt Buộc",
                minlength: "Chứa Ít Nhất 2 Ký Tự"
            },
            val_icon: {
                required: "Bắt Buộc"
            },
            val_description: {
                required: "Bắt Buộc"
            }
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //---------------------------------MEDIATYPE VALIDATE--------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var mediaAddForm = $('#addmedia_frm');

    mediaAddForm.validate({
        errorClass: "help-block animation-slideDown",
        errorElement: "span",
        errorPlacement: function (e, a) {
            a.parents(".form-group > div").append(e)
        },
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success has-error").addClass("has-error"),
            $(e).closest(".help-block").remove()
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-success has-error"),
            e.closest(".help-block").remove()
        }, rules: {
            val_name: {
                required: !0,
                minlength: 2
            },
            val_icon: {
                required: !0,
                minlength: 2
            },
            val_description: {
                required: !0
            },
            val_mime:{
                required: !0
            }
        }, messages: {
            val_name: {
                required: "Bắt Buộc",
                minlength: "Chứa Ít Nhất 2 Ký Tự"
            },
            val_icon: {
                required: "Bắt Buộc"
            },
            val_description: {
                required: "Bắt Buộc"
            },
            val_mime: {
                required: "Bắt Buộc"
            }
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    var proAddForm = $('#product_add_form');
    var errorProAdd = $('.alert-error', proAddForm);

    proAddForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            name: {
                minlength: 2,
                required: true
            },
            description: {
                required: true
            },
            score: {
                number: true,
                min: 1
            },
            quantity: {
                number: true
            }
        },
        messages: {
        },

        errorPlacement: function (error, element) { // render error placement for each input type

            error.insertAfter(element); // for other inputs, just perform default behavoir

        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorProAdd.show();
            App.scrollTo(errorCatAdd, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorProAdd.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var proEditForm = $('#product_edit_form');
    var errorProEdit = $('.alert-error', proEditForm);

    proEditForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            name: {
                minlength: 2,
                required: true
            },
            description: {
                required: true
            },
            score: {
                number: true
            },
            quantity: {
                number: true
            }
        },
        messages: {
        },

        errorPlacement: function (error, element) { // render error placement for each input type

            error.insertAfter(element); // for other inputs, just perform default behavoir

        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorProEdit.show();
            App.scrollTo(errorCatAdd, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorProEdit.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    var adminAddForm = $('#admin_add_form');
    var errorAdminAdd = $('.alert-error', adminAddForm);

    adminAddForm.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",
        rules: {
            username: {
                minlength: 6,
                regexp: "account",
                required: true
            },
            password: {
                minlength: 6,
                regexp: "password",
                required: true
            },
            confirmpassword: {
                equalTo: "#password",
                regexp: "password",
                minlength: 6,
                required: true
            },
            firstname: {
                regexp: "fname",
                required: true
            },
            lastname: {
                regexp: "lname",
                required: true
            },
            gender: {
                required: true
            },
            email: {
                email: true,
                required: true
            }
        },
        messages: {
            gender: {
                required: "Please select a gender choice."
            },
            confirmpassword: {
                equalTo: "Your confirm password incorrect."
            }
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            if (element.attr("name") == "gender") { // for chosen elements, need to insert the error after the chosen container
                error.insertAfter("#frm_gender_error");
            } else {
                error.insertAfter(element); // for other inputs, just perform default behavoir
            }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit  
            errorAdminAdd.show();
            App.scrollTo(errorMemAdd, -100);
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
            .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },

        submitHandler: function (form) {
            errorAdminAdd.hide();
        }
    });

    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------

    $("#ad-info").validate({
        errorClass: "help-block animation-slideDown",
        errorElement: "span",
        errorPlacement: function (e, a) {
            a.parents(".form-group > div").append(e)
        },
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success has-error").addClass("has-error"),
            $(e).closest(".help-block").remove()
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-success has-error"),
            e.closest(".help-block").remove()
        }, rules: {
            val_title: {
                required: !0,
                minlength: 2
            },
            val_pname: {
                required: !0,
                minlength: 2
            },
            val_mediatype: {
                required: !0
            },
            val_publisher: {
                required: !0
            },
            val_categories: {
                required: !0
            },
            val_prethum: {
                required: !0
            },
            val_shortdes: {
                required: !0
            }
        }, messages: {
            val_title: {
                required: "Bắt Buộc",
                minlength: "Chứa Ít Nhất 2 Ký Tự"
            },
            val_pname: {
                required: "Bắt Buộc",
                minlength: "Chứa Ít Nhất 2 Ký Tự"
            },
            val_mediatype: {
                required: "Bắt Buộc"
            },
            val_publisher: {
                required: "Bắt Buộc"
            },
            val_categories: {
                required: "Bắt Buộc"
            },
            val_prethum: {
                required: "Bắt Buộc"
            },
            val_shortdes: {
                required: "Bắt Buộc"
            }
        }

    })
});